package com.flyduck.web;

import com.flyduck.domain.SysRole;
import com.flyduck.domain.SysUser;
import com.flyduck.service.SysRoleService;
import com.flyduck.vo.AjaxResult;
import com.flyduck.vo.PageResult;
import com.flyduck.vo.QueryVo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @program: flyduck-admin
 * @description: 角色管理
 * @author: flyduck
 * @create: 2020-05-06 22:27
 **/
@Controller
@RequestMapping("/role")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 首页
     * @return
     */
    @RequiresPermissions("role:list")
    @GetMapping
    public String index(){
        return "role";
    }

    /**
     * 首页数据
     * @return
     */
    @RequiresPermissions("role:list")
    @PostMapping("/list")
    @ResponseBody
    public PageResult findList(QueryVo queryVo){
       return sysRoleService.findPage(queryVo);
    }



    /**
     * 获取所有的角色
     * @return
     */
    @GetMapping("/all")
    @ResponseBody
    public List<SysRole> getAll(){
        return sysRoleService.findList();
    }


    /**
     * 保存角色
     * @return
     */
    @RequiresPermissions("role:save")
    @GetMapping("/save")
    @ResponseBody
    public AjaxResult save(SysRole sysRole){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            sysRoleService.save(sysRole);
            ajaxResult.setSuccess(true);
            ajaxResult.setMsg("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMsg("保存失败");
        }
        return ajaxResult;
    }

    /**
     * 更新角色
     * @return
     */
    @RequiresPermissions("role:update")
    @GetMapping("/update")
    @ResponseBody
    public AjaxResult update(SysRole sysRole){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            sysRoleService.update(sysRole);
            ajaxResult.setSuccess(true);
            ajaxResult.setMsg("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMsg("更新失败");
        }
        return ajaxResult;
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @RequiresPermissions("role:delete")
    @PostMapping("delete")
    @ResponseBody
    public AjaxResult delete(Long id){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            sysRoleService.deleteById(id);
            ajaxResult.setSuccess(true);
            ajaxResult.setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMsg("删除失败");
        }
        return ajaxResult;
    }

    /**
     * 根据角色id查询角色
     * @return
     */
    @GetMapping("/getRoleById")
    @ResponseBody
    public SysRole getUserById(Long id){
        return sysRoleService.getRoleById(id);
    }

    /**
     * 启用/禁用角色
     * @return
     */
    @RequiresPermissions(value = {"role:enable","role:disable"},logical = Logical.OR)
    @PostMapping("/updateStatus")
    @ResponseBody
    public AjaxResult updateStatus(Long id,Boolean enabled){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            sysRoleService.updateStatus(id,enabled);
            ajaxResult.setSuccess(true);
            ajaxResult.setMsg("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMsg("更新失败");
        }
        return ajaxResult;
    }


}
